package android.test.devicetest;

import java.io.File;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.os.RecoverySystem;
import android.test.until.Max3HWVerion;

public class MountReceiver extends BroadcastReceiver {
    private final String TAG = "MountReceiver";
    private static int first = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        final String action = intent.getAction();
        Log.d(TAG, "------ljw----onReceive ACTION_MEDIA_MOUNTED---------");
        if (Intent.ACTION_MEDIA_MOUNTED.equals(action) && first == 0){
            first = 1;
            installPackage(context, new File("/sdcard/update.bin"));
        }
    }

    private void installPackage(Context context, File packageFile){
        if (!packageFile.exists()){
            Log.d(TAG, "------ljw----packageFile not exists");
        }
        else{
            Log.d(TAG, "-----ljw---packageFile exists");
            Max3HWVerion.runRootCommand("mount -o remount rw /system");
            Max3HWVerion.runRootCommand("rm -rf /system/app/wikango_*.apk");
            Max3HWVerion.runRootCommand("killall -9 com.android.phone");

            try {
                RecoverySystem.installPackage(context, packageFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 
}
